//
//  HomeController.m
//  NormalFramework
//
//  Created by beisheng03 on 2018/3/8.
//  Copyright © 2018年 beisheng. All rights reserved.
//

#import "HomeController.h"
#import "HomeCycleImageCell.h"
#import "ADImageTableCell.h"
#import "HomeProgramaCell.h"
#import "HomeGoodsListCell.h"
#import "HomeNoticeCell.h"
#import "HomeSetionHeader.h"
#import "GoodDetailController.h"
#import "GoodsCategoryVC.h"

#import "HomeDataModel.h"
#import "HomeADModel.h"
#import "HomeNavModel.h"
#import "GoodsInfoModel.h"


#import "HomeContentCell.h"


#import "HomeTableView.h"
@interface HomeController ()<HomeSetionHeaderDelegate,HomeGoodsListCellDelegate>

@property (nonatomic, strong) HomeDataModel *homeData;

@property (nonatomic, weak) HomeTableView *tableView;

@property (nonatomic, strong) HomeContentCell *contentCell;

@property (nonatomic, strong) HomeSetionHeader *titleView;

@property (nonatomic, assign) BOOL canScroll;

@end

@implementation HomeController


- (HomeTableView *)tableView{
    
    if (!_tableView) {
        HomeTableView *temp = [[HomeTableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView = temp;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        //_tableView.bounces = NO;
        [self.view addSubview:_tableView];
    }
    
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    

    
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    if (@available(iOS 11.0, *)) {
        
        self.tableView.estimatedRowHeight = 200;
        self.tableView.estimatedSectionFooterHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        
    }
    
    
    self.tableView.contentInset = UIEdgeInsetsMake(IPhoneX ? 88 : 64, 0, IPhoneX ? 83 : 49, 0);
    
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeCycleImageCell class]) bundle:nil] forCellReuseIdentifier:[HomeCycleImageCell identifierStr]];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ADImageTableCell class]) bundle:nil] forCellReuseIdentifier:[ADImageTableCell identifierStr]];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeProgramaCell class]) bundle:nil] forCellReuseIdentifier:[HomeProgramaCell identifierStr]];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeContentCell class]) bundle:nil] forCellReuseIdentifier:[HomeContentCell identifierStr]];

    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeNoticeCell class]) bundle:nil] forCellReuseIdentifier:[HomeNoticeCell identifierStr]];

    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeSetionHeader class]) bundle:nil] forHeaderFooterViewReuseIdentifier:[HomeSetionHeader identifierStr]];
    
    self.canScroll = YES;
    
    MJWeakSelf
    self.tableView.mj_header = [XMGRefreshHeader headerWithRefreshingBlock:^{
        
        
        [weakSelf loadData];
        
    }];
    
    [self.tableView.mj_header beginRefreshing];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeScrollStatus) name:@"leaveTop" object:nil];



}






#pragma mark notify
- (void)changeScrollStatus//改变主视图的状态
{
    self.canScroll = YES;
    self.contentCell.cellCanScroll = NO;
}

- (void)loadData{


    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:POST_METHOD URLPath:@"index.php/Api/Dbindex/index" parameters:nil completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            
            
            weakSelf.homeData = [HomeDataModel mj_objectWithKeyValues:responseObject[@"data"]];
            
            [weakSelf.tableView.mj_header endRefreshing];
            
            [weakSelf.tableView reloadData];
            
        } else {
            
            [weakSelf.tableView.mj_header endRefreshing];

            [SVProgressHUD showErrorWithStatus:error.domain];

        }
        
        
        
    }];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if (self.homeData == nil) {
        return 0;
    }
    
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (self.homeData == nil) {
        return 0;
    }
    
    if (section == 0) {
        return 2;
    } else {
        return 1;
    }
   
   
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
             HomeCycleImageCell *cell = [tableView dequeueReusableCellWithIdentifier:[HomeCycleImageCell identifierStr]];
            cell.urls = [self.homeData.multi_adv valueForKeyPath:@"adv_image"];
            return  cell;
        } else {
            
            ADImageTableCell *cell = [tableView dequeueReusableCellWithIdentifier:[ADImageTableCell identifierStr]];
            cell.model = self.homeData.single_adv;
            return  cell;
            
        }
        
    } else if (indexPath.section == 1){
        
        HomeProgramaCell *cell = [tableView dequeueReusableCellWithIdentifier:[HomeProgramaCell identifierStr]];
        cell.models = self.homeData.nav_list;
        return  cell;
        
    }else if (indexPath.section == 2){
        

            HomeNoticeCell *cell = [tableView dequeueReusableCellWithIdentifier:[HomeNoticeCell identifierStr]];
            return cell;

        
    }else if (indexPath.section == 3){
        
        HomeContentCell *cell = [tableView dequeueReusableCellWithIdentifier:[HomeContentCell identifierStr]];
        
        
        return cell;


        
    }else{
        
        BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[BaseTableViewCell identifierStr]];
        
        
        return cell;
        
        
    }
 
}






- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    

    
    
}


- (void)homeProgramaBtnDidClickWith:(NSInteger)index{
    
    if (index == 0) {
        
        GoodsCategoryVC *temp = [[GoodsCategoryVC alloc]init];
        
        [self.navigationController pushViewController:temp animated:YES];
        
    } else if (index == 1) {
        
    }else if (index == 2) {
        
    }else if (index == 3) {
        
    }
  
    
}



- (void)homeGoodsBtnDidClickWithModel:(NSObject *)obj{
    
    
    GoodDetailController *temp = [[GoodDetailController alloc]init];
    
    
    [self.navigationController pushViewController:temp animated:YES];
    
    
    
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
           
            return  KScreenW / 69.0 * 28.0 + 16;
            
        } else if (indexPath.row == 1){
            
            return  130;

            
        }else if (indexPath.row == 1){
            
            return  108;
            
            
        }else {
            
           return  54;
            
            
        }
        
    }else{
        
        
        return  KScreenH - (IPhoneX ? 88 : 64);

 
        
    }
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    if (section == 1) {
        return 36;
    } else {
        return 0;
    }
    
    
  
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
    
    
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
      return [[UIView alloc]init];
    
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    if (section == 1) {
        
        HomeSetionHeader *temp = (HomeSetionHeader *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:[HomeSetionHeader identifierStr]];
        
        self.titleView = temp;
        
        temp.delegate = self;
        
        return temp;
    } else {
        return [[UIView alloc]init];
    }
    
    


//    if (self.setionCount == 3) {
//
//        if (section == 2) {
//

//        } else {
//
//             return [[UIView alloc]init];
//
//        }
//
//    } else {
//        if (section == 3) {
//
//            HomeSetionHeader *temp = (HomeSetionHeader *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:[HomeSetionHeader identifierStr]];
//
//            temp.delegate = self;
//
//            return temp;
//
//        } else {
//
//            return [[UIView alloc]init];
//        }
//    }
}


- (void)btnDidClickWith:(UIButton *)btn{
//    @[@"人气",@"最新",@"进度",@"总需人次"];
    if ([btn.currentTitle isEqualToString:@"人气"]) {
//        if (self.seletIndex == 0) {
//
//        } else {
//            self.seletIndex = 0;
//           [self.tableView reloadData];
//        }
//
    } else if ([btn.currentTitle isEqualToString:@"最新"]) {
    
    }else if ([btn.currentTitle isEqualToString:@"进度"]) {
       
    }else if ([btn.currentTitle isEqualToString:@"总需人次"]) {
    

    }
    
 
    
}

#pragma mark UIScrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    
    if (self.homeData == nil) {
        return;
    }
    CGFloat bottomCellOffset = [self.tableView rectForSection:1].origin.y - (IPhoneX ? 88 : 64);
    if (scrollView.contentOffset.y >= bottomCellOffset) {
        scrollView.contentOffset = CGPointMake(0, bottomCellOffset);
        if (self.canScroll) {
            self.canScroll = NO;
            self.contentCell.cellCanScroll = YES;
        }
    }else{
        if (!self.canScroll) {//子视图没到顶部
            scrollView.contentOffset = CGPointMake(0, bottomCellOffset);
        }
    }
//    self.tableView.showsVerticalScrollIndicator = _canScroll ? YES : NO;
}





@end
